import http from 'src/api/http';
import { SystemAPI } from 'src/api/urls';
import {
  IMenuTreeRequestParams,
  IMenuRespData,
  IMenuHandleRequestData,
  IMenuHandleRespData,
  IRoleRequestParams,
  IRoleRespData,
  IBaseTableResonseWrap,
  IRoleActionRequestData,
  IDeptRequestParam,
  IDeptRespData,
  IDeptActionRequestData,
} from 'src/interface';

export const getMenuTree = (params?: IMenuTreeRequestParams) => {
  return http.get<IMenuRespData[]>(SystemAPI.MENU_TREE, { params });
};

export const AddMenu = (data: IMenuHandleRequestData) => {
  return http.post<IMenuHandleRespData>(SystemAPI.MENU_ADD, { data });
};

export const UpdateMenu = (data: IMenuHandleRequestData) => {
  return http.put<IMenuHandleRespData>(SystemAPI.MENU__UPDATE, { data });
};

export const DeleteMenu = (id: string) => {
  return http.delete(`${SystemAPI.MENU_DELETE}/${id}`);
};

export const getRoleList = (params: IRoleRequestParams) => {
  return http.get<IBaseTableResonseWrap<IRoleRespData>>(SystemAPI.ROLE_LIST, { params });
};

export const AddRole = (data: IRoleActionRequestData) => {
  return http.post<IRoleRespData>(SystemAPI.ROLE_ADD, { data });
};

export const UpdateRole = (data: IRoleActionRequestData) => {
  return http.put<IRoleRespData>(SystemAPI.ROLE_UPDATE, { data });
};

export const DeleteRole = (data: string[]) => {
  return http.delete(SystemAPI.ROLE_DELETE_SELECT, { data });
};

export const UpdateRoleToMenus = (data: { roleId: string; menuIds: string[] }) => {
  const { roleId, menuIds } = data;
  return http.put(SystemAPI.ROLE_UPDATE_MENUS, { data: { roleId, menuIds: menuIds.join(',') } });
};

export const getDeptTree = (params: IDeptRequestParam) => {
  return http.get<IDeptRespData[]>(SystemAPI.DEPT_TREE, { params });
};

export const AddDept = (data: IDeptActionRequestData) => {
  return http.post(SystemAPI.DEPT_ADD, { data });
};

export const UpdateDept = (data: IDeptActionRequestData) => {
  return http.put(SystemAPI.DEPT_UPDATE, { data });
};

export const DeleteDept = (id: string) => {
  return http.delete(`${SystemAPI.DEPT_DELETE}/${id}`);
};
